package net.sppan.blog.utils;

import java.util.UUID;

public class TransactionIdUtil {
	public static String compressedUUID() {
		UUID uuid = UUID.randomUUID();
		byte[] byUuid = new byte[16];
		long least = uuid.getLeastSignificantBits();
		long most = uuid.getMostSignificantBits();
		long2bytes(most, byUuid, 0);
		long2bytes(least, byUuid, 8);
		String compressUUID = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(byUuid);
		return compressUUID + System.currentTimeMillis();
	}

	private static void long2bytes(long value, byte[] bytes, int offset) {
		for (int i = 7; i > -1; i--) {
			bytes[offset++] = (byte) ((value >> 8 * i) & 0xFF);
		}
	}

}
